<?php

/* * ************************************************************************* 
 * index.php
 * Encoding : UTF-8
 * Desc. :
 * Created on : 2013-4-23 16:05:56 by Aiden , aiden.ke@doosnet.net
 * Updated on : 2013-4-23 16:05:56 by Aiden , aiden.ke@doosnet.net
 * ************************************************************************* */
defined('IN_SPIDER') or exit('No permission resources.');

class node {

    public function __construct() {
        pc_base::load_app_class('RollingCurl', 'collection', 0);
        pc_base::load_app_func('global');
    }

    /**
     * 销售、价格、评分更新
     */
    public function daily() {
        $rid = $mid = $sid = $num = null;

        //请求标识ID
        if (isset($_REQUEST['rid']) && !empty($_REQUEST['rid']) && is_numeric($_REQUEST['rid'])) {
            $rid = $_REQUEST['rid'];
        } else {
            showmessage('请输入请求ID', 1);
        }

        //商城标识ID
        if (isset($_REQUEST['mid']) && !empty($_REQUEST['mid']) && is_numeric($_REQUEST['mid'])) {
            $mid = $_REQUEST['mid'];
        } else {
            showmessage('请输入商城标识ID', 1);
        }

        //起始ID
        $sid = is_numeric($_REQUEST['sid']) ? $_REQUEST['sid'] : 0;

        //查询数量
        $num = is_numeric($_REQUEST['num']) ? $_REQUEST['num'] : 500;

        //若为天猫商城，则加载Api库
//        if ($sid == '152') {
//            require_once APP_PATH . 'libs/api/Tmall.php';
//            require_once APP_PATH . 'libs/api/TopSdk.php';
//        }
        //表名
        $tablename = pc_base::load_config('tablemap', $mid);

        $this->recheck_parsing_db = pc_base::load_model('recheck_parsing_model');
        $sql = "select count(*) cnt,currentid,endid,remark from shop_recheck_parsing where id='{$rid}';";
        $data = $this->recheck_parsing_db->query($sql);

        if (!$data['0']['cnt']) {//第一次执行要插入记录 
            $startId = $this->get_tid($tablename, $sid); //获得起始时间   
            if ($startId) {
                $endId = $this->get_tid($tablename, intval($sid + $num), TRUE); //获得结束时间    
                $remark = $rid . '|' . $mid . '|' . $sid . '|' . $num; //备注
                $sql = "INSERT INTO `shop_recheck_parsing` VALUES ('{$rid}', '{$mid}', '{$startId}', '{$startId}', '{$endId}','{$remark}');"; //插入记录
                $this->recheck_parsing_db->query($sql);
                $this->shelf_checks($rid, $mid, $startId, $endId, $num); //检查下架
            }
        } else {
            $num = explode('|', $data['0']['remark']);
            $num = end($num);
            $this->shelf_checks($rid, $mid, $data['0']['currentid'], $data['0']['endid'], $num);
        }
        unset($rid, $mid, $sid, $num, $sql, $data, $startId, $endId, $remark);
    }

    /**
     * 获得最后一次更新时间
     * @param tableNmae $string 表名
     * @param startId $string 起始Id
     * @param end $string 是否获取结束ID
     * @return string 返回最后一次更新时间
     */
    public function get_tid($tableName, $startId, $end=FALSE) {

        $tableName = $tableName . '_special';
        $data = array();
        $end && $startId = intval($startId - 1);
        $this->item_special_model_db = pc_base::load_model('item_special_model');
        $sql = "select `last_update` from `$tableName` where UNIX_TIMESTAMP()>`last_update`+" . pc_base::load_config('system', 'cycleTime') . " and `shelfviews`<" . pc_base::load_config('system', 'shelfviews') . " and is_del=0 ORDER BY `last_update` ASC LIMIT $startId,1;";
        $data = $this->item_special_model_db->query($sql);
        if ($end) {//获取符合条件的最后一个last_update
            if (!isset($data['0']['last_update'])) {
                unset($data);
                $sql = "select `last_update` from `$tableName` where UNIX_TIMESTAMP()>`last_update`+" . pc_base::load_config('system', 'cycleTime') . " and `shelfviews`<" . pc_base::load_config('system', 'shelfviews') . " and is_del=0 ORDER BY `last_update` DESC LIMIT 0,1;";
                $data = $this->item_special_model_db->query($sql);
            }
        }
        if (!isset($data['0']['last_update'])) {//若不存在时，返回0
            $data['0']['last_update'] = 0;
        }
        return $data['0']['last_update'];
    }

    /**
     * 检查下架、更新价格函数
     * @param requestId $string 请求ID
     * @param selId $string 商城标识ID
     * @param startId $string 起始item_no
     * @param endId $string 结束item_no
     */
    public function shelf_checks($requestId, $selId, $startId, $endId, $lgh) {
        global $item_no,$startTime;
        $item_no = 0;
        $startTime=getmicrotime(TRUE);
        
        //表名
        $tablename = pc_base::load_config('tablemap', $selId);

        if ($startId != $endId) {
            $where = "AND `last_update`>={$startId} AND `last_update`<={$endId} order by `last_update`";
        } else {
            $where = "AND `last_update`={$startId}";
        }

        $this->item_special_model_db = pc_base::load_model('item_special_model');
        $sql = " select `item_no`,`goods_id`,`mall_id`,`goods_url`,`shelfviews`,`last_update` from `" . $tablename . "_special` where `mall_id`={$selId} AND UNIX_TIMESTAMP() > `last_update`+" . pc_base::load_config('system', 'cycleTime') . "  AND `shelfviews`<" . pc_base::load_config('system', 'shelfviews') . " AND is_del=0 {$where} LIMIT {$lgh};";
        $datalist = $this->item_special_model_db->query($sql);

        $num_results = count($datalist);

        if (!$num_results) {
            $this->recheck_parsing_db = pc_base::load_model('recheck_parsing_model');
            $this->recheck_parsing_db->delete_request_records($requestId); //删除请求记录
        } else {
            //RollingCurl 批处理采集
            $rc = new RollingCurl('parser_shelf');
            $rc->window_size = 10;
            foreach ($datalist as $row) {
                $request = new RollingCurlRequest($row['goods_url']);
                $request->data = $row;
                $rc->add($request);
            }
            $rc->execute();

            $this->item_special_model_db = pc_base::load_model('item_special_model');
            $this->item_special_model_db->update_shelfviews($selId, '', $item_no);
            
            $this->recheck_parsing_db = pc_base::load_model('recheck_parsing_model');
            $this->recheck_parsing_db->delete_request_records($requestId); //删除请求记录
        }
    }
    
    public function __destruct() {
        echo "<br/>运行时间" . execute_time() . "<br />\n";
    }

}

?>
